/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Tiec;

import Server.BUS.CTPhieuDatTiec_BUS;
import Server.BUS.KhachHang_BUS;
import Server.BUS.MonAn_BUS;
import Server.BUS.PhieuDatTiec_BUS;
import DTO.CTPhieuDatTiec_DTO;
import DTO.KhachHang_DTO;
import DTO.PhieuDatTiec_DTO;
import Define.Define;
import GUI.MyTableModel;
import GUI.ShowReport;
import GUI.ThuePhong.JIFDatPhong;
import GUI.Utilities;
import Server.QLKS_Interface;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr
 */
public class JIFDatTiec extends javax.swing.JInternalFrame {

    DefaultComboBoxModel cbbModel_tiec;
    DefaultTableModel tbModel_tiec;
    ArrayList<KhachHang_DTO> ds_tiec = new ArrayList<>();
    Vector tableTitle_tiec = new Vector();
    Vector tableRecords_tiec = new Vector();
    String ServerURL;
    QLKS_Interface QI;

    /**
     * Creates new form JIFDatTiec
     */
    public JIFDatTiec() {
        initComponents();
        this.setSize(880, 560);
        ServerURL = "qlks";
        try {
            QI = (QLKS_Interface) Naming.lookup(ServerURL);
        } catch (NotBoundException | MalformedURLException | RemoteException ex) {
            Logger.getLogger(JIFDatPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        showCbbMaKhach_tiec();
        showBangMonAn();
        showMaPhieuDat_tiec();
        tableTitle_tiec.add("Mã Món Ăn");
        tableTitle_tiec.add("Tên Món Ăn");
        tableTitle_tiec.add("Đơn Giá");
        tableTitle_tiec.add("Số Lượng");
        tablePhieuDatTiec_tiec.setModel(new MyTableModel(tableRecords_tiec, tableTitle_tiec));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbbMaKhach_tiec = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfTenKhach_tiec = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tfSoDienThoai_tiec = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfMaPhieuDat_tiec = new javax.swing.JTextField();
        dpNgayDat_tiec = new org.jdesktop.swingx.JXDatePicker();
        jLabel6 = new javax.swing.JLabel();
        tfThanhTien_tiec = new javax.swing.JTextField();
        btXacNhan_tiec = new javax.swing.JButton();
        btBoMon_tiec = new javax.swing.JButton();
        btThoat = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablePhieuDatTiec_tiec = new org.jdesktop.swingx.JXTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableMonAn_tiec = new org.jdesktop.swingx.JXTable();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("LẬP PHIẾU ĐẶT TIỆC");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin Khách hàng"));
        jPanel1.setToolTipText("");
        jPanel1.setName("Thông tin Khách hàng"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(237, 110));

        cbbMaKhach_tiec.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbMaKhach_tiecItemStateChanged(evt);
            }
        });

        jLabel1.setText("Mã Khách");

        jLabel2.setText("Tên Khách Hàng");

        jLabel3.setText("Số Điện Thoại");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(tfTenKhach_tiec, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                    .addComponent(tfSoDienThoai_tiec, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cbbMaKhach_tiec, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(cbbMaKhach_tiec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(tfTenKhach_tiec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel3)
                    .addComponent(tfSoDienThoai_tiec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin Phiếu đặt tiệc"));
        jPanel2.setPreferredSize(new java.awt.Dimension(450, 110));

        jLabel4.setText("Mã Phiếu Đặt");

        jLabel5.setText("Ngày Đặt");

        tfMaPhieuDat_tiec.setEditable(false);
        tfMaPhieuDat_tiec.setBackground(new java.awt.Color(204, 204, 204));
        tfMaPhieuDat_tiec.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfMaPhieuDat_tiec.setMaximumSize(new java.awt.Dimension(12, 20));
        tfMaPhieuDat_tiec.setMinimumSize(new java.awt.Dimension(12, 20));

        dpNgayDat_tiec.setDate(new Date(System.currentTimeMillis()));
        dpNgayDat_tiec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dpNgayDat_tiecActionPerformed(evt);
            }
        });

        jLabel6.setText("Thành Tiền");

        tfThanhTien_tiec.setEditable(false);
        tfThanhTien_tiec.setBackground(new java.awt.Color(204, 204, 204));
        tfThanhTien_tiec.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfThanhTien_tiec.setText("0");
        tfThanhTien_tiec.setMaximumSize(new java.awt.Dimension(12, 20));
        tfThanhTien_tiec.setMinimumSize(new java.awt.Dimension(12, 20));

        btXacNhan_tiec.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/1371825243_Apply.png"))); // NOI18N
        btXacNhan_tiec.setText("Xác Nhận");
        btXacNhan_tiec.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btXacNhan_tiecMouseEntered(evt);
            }
        });
        btXacNhan_tiec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXacNhan_tiecActionPerformed(evt);
            }
        });

        btBoMon_tiec.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Erase.png"))); // NOI18N
        btBoMon_tiec.setText("Bỏ Món");
        btBoMon_tiec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBoMon_tiecActionPerformed(evt);
            }
        });

        btThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close.png"))); // NOI18N
        btThoat.setText("Thoát");
        btThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(10, 10, 10)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(tfMaPhieuDat_tiec, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5))
                    .addComponent(tfThanhTien_tiec, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dpNgayDat_tiec, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(54, Short.MAX_VALUE)
                .addComponent(btXacNhan_tiec)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btBoMon_tiec)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btThoat)
                .addContainerGap(54, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel4)
                            .addComponent(tfMaPhieuDat_tiec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel6)
                            .addComponent(tfThanhTien_tiec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(jLabel5)
                        .addComponent(dpNgayDat_tiec, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btXacNhan_tiec)
                    .addComponent(btBoMon_tiec)
                    .addComponent(btThoat))
                .addContainerGap())
        );

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Chi tiết Phiếu đăt tiệc"));

        tablePhieuDatTiec_tiec.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tablePhieuDatTiec_tiec);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Món ăn"));

        tableMonAn_tiec.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableMonAn_tiec.setEditable(false);
        tableMonAn_tiec.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMonAn_tiecMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableMonAn_tiec);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 387, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbbMaKhach_tiecItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbMaKhach_tiecItemStateChanged
        try {
            // TODO add your handling code here:
            //tfTenKhach_tiec.setText(KhachHang_BUS.layTenKHTheoMa(cbbMaKhach_tiec.getSelectedItem().toString()));
            tfTenKhach_tiec.setText(QI.LayTenKHTheoMa(cbbMaKhach_tiec.getSelectedItem().toString()));
        } catch (RemoteException ex) {
            Logger.getLogger(JIFDatTiec.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            // TODO add your handling code here:
            //tfSoDienThoai_tiec.setText(KhachHang_BUS.laySoDTTheoMa(cbbMaKhach_tiec.getSelectedItem().toString()));
            tfSoDienThoai_tiec.setText(QI.LaySoDTTheoMa(cbbMaKhach_tiec.getSelectedItem().toString()));
        } catch (RemoteException ex) {
            Logger.getLogger(JIFDatTiec.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cbbMaKhach_tiecItemStateChanged

    private void dpNgayDat_tiecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dpNgayDat_tiecActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_dpNgayDat_tiecActionPerformed

    private void btXacNhan_tiecMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btXacNhan_tiecMouseEntered
        // TODO add your handling code here:
        //btXacNhan.setBackground(Color.red);
    }//GEN-LAST:event_btXacNhan_tiecMouseEntered

    private void btXacNhan_tiecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXacNhan_tiecActionPerformed
        // TODO add your handling code here:
        String maKhach = cbbMaKhach_tiec.getSelectedItem().toString();
        String maPhieuDat = tfMaPhieuDat_tiec.getText();
        String thanhTien = tfThanhTien_tiec.getText();
        Date ngay = dpNgayDat_tiec.getDate();
        SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
        String ngayDat = format.format(ngay);
        Date today = new Date(System.currentTimeMillis() - 100742784);
        if (tablePhieuDatTiec_tiec.getRowCount() == 0) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa chọn món ăn!");
            return;
        }
        if (thanhTien.equals("0")) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa chọn số lượng món ăn!");
            return;
        }
        if (ngayDat == null) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa chọn ngày đặt tiệc!");
            return;
        }
        int sosanh = ngay.compareTo(today);
        if (sosanh < 0) {
            JOptionPane.showMessageDialog(rootPane, "Ngày đặt phải bằng hoặc sau hiện tại!");
            return;
        }
        try {
            PhieuDatTiec_DTO p = new PhieuDatTiec_DTO();
            p.setMaKhach(maKhach);
            p.setMaPhieuDatTiec(maPhieuDat);
            p.setTriGia(thanhTien);
            p.setNgayDat(ngayDat);
            //PhieuDatTiec_BUS.Insert(p);
            QI.InsertPhieuDatTiec(p);
            for (int i = 0; i < tablePhieuDatTiec_tiec.getRowCount(); i++) {
                int soLuong = Integer.parseInt(tablePhieuDatTiec_tiec.getValueAt(i, 3).toString());
                Float donGia = Float.parseFloat(tablePhieuDatTiec_tiec.getValueAt(i, 2).toString());
                Float tien = (Float) donGia * soLuong;
                String maCTPhieuDatTiec = maCTPhieuDatTiec();
                CTPhieuDatTiec_DTO ct = new CTPhieuDatTiec_DTO();
                ct.setMaCTPhieuDatTiec(maCTPhieuDatTiec);
                ct.setMaMonAn(tablePhieuDatTiec_tiec.getValueAt(i, 0).toString());
                ct.setMaPhieuDatTiec(maPhieuDat);
                ct.setThanhTien(tien.toString());
                ct.setSoLuong(soLuong);
                //CTPhieuDatTiec_BUS.Insert(ct);
                QI.InsertCTPhieuDatTiec(ct);
            }
            JOptionPane.showMessageDialog(rootPane, "Đặt tiệc thành công");
            new ShowReport().Show_ReportDatTiecRMI(maPhieuDat);
            tableRecords_tiec.clear();
            tablePhieuDatTiec_tiec.setModel(new MyTableModel(tableRecords_tiec, tableTitle_tiec));
            tfThanhTien_tiec.setText("0");
            showMaPhieuDat_tiec();
        } catch (Exception ex) {
            Logger.getLogger(JIFDatTiec.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btXacNhan_tiecActionPerformed

    private void btBoMon_tiecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBoMon_tiecActionPerformed
        // TODO add your handling code here:
        int row = tablePhieuDatTiec_tiec.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(rootPane, "Bạn chưa chọn món ăn!");
            return;
        }
        tableRecords_tiec.remove(row);
        tablePhieuDatTiec_tiec.setModel(new MyTableModel(tableRecords_tiec, tableTitle_tiec));
        String strThanhTien = String.format("%f", tinhThanhTien());
        tfThanhTien_tiec.setText(strThanhTien);
    }//GEN-LAST:event_btBoMon_tiecActionPerformed

    private void tableMonAn_tiecMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMonAn_tiecMouseClicked
        // TODO add your handling code here:
        int row = this.tableMonAn_tiec.getSelectedRow();
        for (int i = 0; i < tablePhieuDatTiec_tiec.getRowCount(); i++) {
            if (tablePhieuDatTiec_tiec.getValueAt(i, 0) == tableMonAn_tiec.getValueAt(row, 0)) {
                return;
            }
        }
        if (row < 0 || row > tableMonAn_tiec.getRowCount()) {
            return;
        }
        Vector monAn = new Vector();
        monAn.add((String) this.tbModel_tiec.getValueAt(row, 0).toString());
        monAn.add((String) this.tbModel_tiec.getValueAt(row, 1).toString());
        monAn.add((String) this.tbModel_tiec.getValueAt(row, 2).toString());
        monAn.add("1");
        tableRecords_tiec.add(monAn);
        tablePhieuDatTiec_tiec.setModel(new MyTableModel(tableRecords_tiec, tableTitle_tiec));
        String strThanhTien = String.format("%f", tinhThanhTien());
        tfThanhTien_tiec.setText(strThanhTien);
        tablePhieuDatTiec_tiec.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                String strThanhTien = String.format("%f", tinhThanhTien());
                tfThanhTien_tiec.setText(strThanhTien);
            }
        });
    }//GEN-LAST:event_tableMonAn_tiecMouseClicked

    private void btThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btThoatActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBoMon_tiec;
    private javax.swing.JButton btThoat;
    private javax.swing.JButton btXacNhan_tiec;
    private javax.swing.JComboBox cbbMaKhach_tiec;
    private org.jdesktop.swingx.JXDatePicker dpNgayDat_tiec;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.swingx.JXTable tableMonAn_tiec;
    private org.jdesktop.swingx.JXTable tablePhieuDatTiec_tiec;
    private javax.swing.JTextField tfMaPhieuDat_tiec;
    private javax.swing.JTextField tfSoDienThoai_tiec;
    private javax.swing.JTextField tfTenKhach_tiec;
    private javax.swing.JTextField tfThanhTien_tiec;
    // End of variables declaration//GEN-END:variables

    private void showCbbMaKhach_tiec() {
        cbbModel_tiec = (DefaultComboBoxModel) cbbMaKhach_tiec.getModel();
        try {
            //ds_tiec = KhachHang_BUS.layKhachDangThue();
            ds_tiec = QI.LayDSKhachDangThue();
        } catch (RemoteException e) {
        }

        for (KhachHang_DTO lp : ds_tiec) {
            cbbModel_tiec.addElement(lp.getMaKhachHang());
        }
    }

    private void showBangMonAn() {
        try {
            //tbModel_tiec = MonAn_BUS.layBangMonAn();
             tbModel_tiec = QI.LoadTableModel(Define.YC_LAYDS_MONAN);
            tableMonAn_tiec.setModel(tbModel_tiec);
        } catch (RemoteException e) {
        }
    }

    private void showMaPhieuDat_tiec() {
        try {
            //String lastID = PhieuDatTiec_BUS.layMaCuoiCung();
            String lastID = QI.LayMaMax(Define.YC_LAYMAMAX_PHIEUDATTIEC);
            String nextID = null;
            if (lastID != null) {
                nextID = Utilities.taoMaTuDong(lastID, 4);
            } else {
                nextID = Utilities.taoMaTuDong("MPDT0000", 4);
            }
            tfMaPhieuDat_tiec.setText(nextID);
        } catch (Exception e) {
        }
    }

    private String maCTPhieuDatTiec() throws Exception {
        //String lastID = CTPhieuDatTiec_BUS.layMaCuoiCung();
        String lastID = QI.LayMaMax(Define.YC_LAYMAMAX_CTPHIEUDATTIEC);
        String nextID = null;
        try {
            if (lastID != null) {
                nextID = Utilities.taoMaTuDong(lastID, 4);
            } else {
                nextID = Utilities.taoMaTuDong("MCTT000000", 4);
            }
        } catch (Exception e) {
        }
        return nextID;
    }

    private Float tinhThanhTien() {
        Float thanhTien = 0f;
        for (int i = 0; i < tablePhieuDatTiec_tiec.getRowCount(); i++) {
            int soLuong = Integer.parseInt(tablePhieuDatTiec_tiec.getValueAt(i, 3).toString());
            Float donGia = Float.parseFloat(tablePhieuDatTiec_tiec.getValueAt(i, 2).toString());
            thanhTien += soLuong * donGia;
        }
        return thanhTien;
    }
}
